/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.service.document;

import com.documents.common.core.service.BaseSrv;
import com.documents.common.util.FileStorage;
import com.documents.common.util.SystemConfig;
import com.documents.pojo.Accessory;
import com.documents.pojo.DocumentFormatOrder;
import com.documents.pojo.Document;
import com.documents.service.updown.FileUpDownService;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

/**
 *
 * @author Administrator
 */
public class DocumentFormatOrderService extends BaseSrv<DocumentFormatOrder, Integer> {

    private static final Log log = LogFactory.getLog(DocumentFormatOrder.class);
    private DocumentService documentService;
    private FileUpDownService fileUpDownService;
    String webAppPath = "";
    String rootDir = SystemConfig.getInstance().getvalue("DOC_CONVERT_PATH");

    public DocumentService getDocumentService() {
        return documentService;
    }

    public void setDocumentService(DocumentService documentService) {
        this.documentService = documentService;
    }

    public FileUpDownService getFileUpDownService() {
        return fileUpDownService;
    }

    public void setFileUpDownService(FileUpDownService fileUpDownService) {
        this.fileUpDownService = fileUpDownService;
    }

    /**
     * 创建转换工单
     * @param id
     */
    public void createConverOrder(Integer id) {
        String sql = "delete from document_format_order where doc_id=";
        sql += id;
        this.jdbcTemplate.execute(sql);
        sql = "insert into document_format_order(doc_id,status,create_time) values(";
        sql += id + ",0,now())";
        this.jdbcTemplate.execute(sql);
    }

    public void convertToPdf() {
        //begin
        int id = getConverPdfOrder();
        //status=0 等待转换的
        if (id > 0) {
            this.beginConverPdfOrder(id);
            Document doc = documentService.get(id);
            List<Accessory> alist = fileUpDownService.getAccessoryByTableNameAndTableId("document", id);
            if (alist != null && alist.size() > 0) {

                for (Accessory ay : alist) {


                    String fromPath = rootDir + ay.getUrl();
                    log.debug(fromPath);
                    String fileName = fromPath.substring(0, fromPath.indexOf("."));
                    File fromFile = new File(fromPath);
                    JOD4DocToPDF tools = new JOD4DocToPDF(fromFile,
                            new File(fileName + ".pdf"), id);
                    tools.start();

                }
            }
            //end 
        }
    }

    public int getConverPdfOrder() {
        String sql = "select min(doc_id) from document_format_order where status=0 order by create_time asc";
        return this.getJdbcTemplate().queryForInt(sql);
    }

    public void beginConverSwfOrder(Integer id) {
        String sql = "update document_format_order set status=3,FORMAT_Swf_BEGIN_TIME=now() where doc_id=";
        sql += id;
        this.getJdbcTemplate().execute(sql);
    }

    public void endConverPdfOrder(Integer id) {
        String sql = "update document_format_order set status=2,format_pdf_end_time=now() where doc_id=";
        sql += id;
        this.jdbcTemplate.execute(sql);
    }

    public void beginConverPdfOrder(Integer id) {
        String sql = "update document_format_order set status=1,FORMAT_PDF_BEGIN_TIME=now() where doc_id=";
        sql += id;
        this.jdbcTemplate.execute(sql);
    }

    public void convertPdfToSwf() {
        //begin
        int id = getConverSwfOrder();
        //status=2 转pdf成功的
        if (id > 0) {
            this.beginConverSwfOrder(id);
            Document doc = documentService.get(id);
            PdfToSwf s = new PdfToSwf();
            List<Accessory> alist = fileUpDownService.getAccessoryByTableNameAndTableId("document", id);
            if (alist != null && alist.size() > 0) {
                for (Accessory ay : alist) {

                    String startPath = rootDir+ay.getUrl();
                    String fromPath = startPath.substring(0, startPath.indexOf(".")) + ".pdf";
                    String toPath = startPath.substring(0, startPath.indexOf(".")) + ".swf";
                    try {
                        s.convertPDF2SWF(fromPath, toPath);
//                doc.setStatus(DocInfo.STATUS_SUCCESS);

                    } catch (IOException e) {
                        log.error(e.getMessage(), e);
//                doc.setStatus(DocInfo.STATUS_FAIL);
                    }
                }
            }
            //end
            this.endConverSwfOrder(id);
        }
    }

    public int getConverSwfOrder() {
        String sql = "select min(doc_id) from document_format_order where status=2 order by create_time asc";
        return this.jdbcTemplate.queryForInt(sql);
    }

    public void endConverSwfOrder(Integer id) {
        String sql = "update document_format_order set status=4,FORMAT_Swf_END_TIME=now() where doc_id=";
        sql += id;
        this.jdbcTemplate.execute(sql);
    }
}
